package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.framework.api.entity.StatusCode;
import org.rcy.ruicingmarket.login.CaptchaService;
import org.rcy.ruicingmarket.exception.OssException;
import org.rcy.ruicingmarket.common.utils.ImageValidateCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author 吴康桥
 * @Description
 * @date 2022/10/10 11:54
 */

@RestController
@RequestMapping("/login")
@Tag(name = "osslogin", description = "后台登录控制器")
public class OssLoginController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CaptchaService captchaService;

	@Operation(summary = "获取图片验证码", description = "根据SID获取图片验证码", tags = {"osslogin"})
	@Parameter(name = "SID", required = true , in = ParameterIn.HEADER)
	@GetMapping("/pin")
	public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
		try {
			String sid = request.getHeader("SID");
			if(StringUtils.isBlank(sid)){
				throw new OssException("请求头错误");
			}
			String code = ImageValidateCodeUtil.outputVerifyImage(100, 40, response.getOutputStream(), 5);
			if(StringUtils.isAllBlank(sid,code)) {
				throw new OssException("生成验证码失败");
			}
			captchaService.cacheCode(sid,code);
		}catch (OssException e){
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	@Operation(summary = "获取SID", description = "获取SID", tags = {"osslogin"})
	@Parameter(name = "SID", required = true , in = ParameterIn.HEADER)
	@GetMapping("/prepare")
	public ResultMessage doPrepare(HttpServletRequest request){
		try {
			String sid = request.getHeader("SID");
			return ResultMessage.success(captchaService.getSID(sid));
		}catch (OssException e){
			logger.error(e.getMessage());
			return ResultMessage.error();
		}
	}

	@Operation(summary = "校验验证码", description = "校验验证码是否正确", tags = {"osslogin"})
	@Parameter(name = "SID", required = true , in = ParameterIn.HEADER)
	@GetMapping("/codeverify")
	public ResultMessage codeVerify(HttpServletRequest request,@Parameter(name = "code", description = "验证码", required = true) @RequestParam("code") String code) {
		try {
			String sid = request.getHeader("SID");
			if(StringUtils.isBlank(sid)){
				throw new OssException("请求头错误");
			}
			if(!captchaService.verifyCode(sid,code)) {
				return ResultMessage.error(StatusCode.VALIDATECODEERROR);
			}
			return ResultMessage.success();
		}catch (OssException e){
			logger.error(e.getMessage());
			return ResultMessage.error();
		}
	}

}
